home *** CD-ROM | disk | FTP | other *** search
- #include <dos.h>
- #include <stddef.h>
- #include "heap.h"
-
- extern hptr HeapPtr;
- extern struct a_heap {
- uint handle;
- ulong size;
- hptr ptr;
- } *Heap;
-
- uint Halloc(ulong bytes)
- {
- uint handle;
-
- if (HeapInitialized) {
- if (bytes) {
- if (bytes <= HeapUnused) {
- for (handle=0; handle<HeapMaxHandles; ++handle) /* Search handles */
- if (Heap[handle].ptr == NULL) /* Found an empty handle */
- break;
-
- if (handle < HeapMaxHandles) { /* Found a valid handle */
- Heap[handle].handle = handle; /* Store new handle */
- Heap[handle].size = bytes; /* Store new size */
- Heap[handle].ptr = HeapPtr; /* Store avail. ptr */
-
- ++*(uint *)&HeapUsedHandles; /* Update variables */
- *(ulong *)&HeapUnused -= bytes;
- *(ulong *)&HeapUsed += bytes;
-
- HeapPtr += bytes; /* Calc next avail. ptr */
-
- HeapError = 0;
- return(handle+1); /* Return new handle */
- }
- else /* Too many handles are used */
- HeapError = ERR_TOOMANYHAND;
- }
- else /* Size too large */
- HeapError = ERR_SIZETOOLARGE;
- }
- else /* Size too small */
- HeapError = ERR_SIZETOOSMALL;
- }
- else /* Not initialized */
- HeapError = ERR_NOTINIT;
-
- return(0);
- }
-